package com.tjetc.service.impl;

import com.tjetc.dao.AdminDao;
import com.tjetc.dao.AdminRoleDao;
import com.tjetc.dao.RoleDao;
import com.tjetc.dao.impl.AdminDaoImpl;
import com.tjetc.dao.impl.AdminRoleDaoImpl;
import com.tjetc.dao.impl.RoleDaoImpl;
import com.tjetc.domain.Admin;
import com.tjetc.domain.AdminRole;
import com.tjetc.domain.Role;
import com.tjetc.service.AdminService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDaoImpl();
    private AdminRoleDao adminRoleDao = new AdminRoleDaoImpl();
    private RoleDao roleDao = new RoleDaoImpl();
    @Override
    public Admin login(String username, String password,int rid) {
        try {
            return adminDao.login(username,password,rid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(String username, String phone) {
        try {
            return adminDao.getCount(username,phone);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Admin> list(String username,String phone,PageInfo<Admin> pageInfo) {
        try {
            List<Admin> list = adminDao.list(username, phone, pageInfo);
            for (Admin admin : list) {
                AdminRole adminRole = adminRoleDao.getByAid(admin.getId());
                Role role = roleDao.getById(adminRole.getRid());
                admin.setRole(role);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Admin admin) {
        try {
            //添加admin和admin_role，需要事务
            Db.startTransaction();
            //添加到admin表
            adminDao.add(admin);
            //找到admin表中最大值
            int aid = adminDao.findMaxId();
            AdminRole adminRole = new AdminRole();
            adminRole.setAid(aid);
            adminRole.setRid(admin.getRole().getId());
            //添加到admin_role表中
            adminRoleDao.add(adminRole);
            Db.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            Db.rollback();
        }finally {
            Db.close();
        }
        return false;
    }

    @Override
    public boolean update(Admin admin) {
        try {
            adminDao.update(admin);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateStatus(int status,int id) {
        try {
            adminDao.updateStatus(status,id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Admin getById(int id) {
        try {
            return adminDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Admin getpwdById(int id) {
        try {
            return adminDao.getpwdById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updatepwd(Admin admin) {
        try {
            return adminDao.updatepwd(admin)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Admin findByName(String username) {
        try {
            return adminDao.findByName(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


}
